package com.lanou.zy.projecta.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.adapter.SrViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class SearchResultFragment extends Fragment {
    private View v;
    private TabLayout searchResultTabLayout;
    private ViewPager searchResultViewPager;

    public static SearchResultFragment newInstance(String name) {
        SearchResultFragment s = new SearchResultFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        s.setArguments(args);
        return s;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_search_result, null);

        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        searchResultTabLayout = (TabLayout) v.findViewById(R.id.searchResultTabLayout);
        searchResultViewPager = (ViewPager) v.findViewById(R.id.searchResultViewPager);
        searchResultTabLayout.setTabMode(searchResultTabLayout.MODE_SCROLLABLE);
        searchResultTabLayout.setTabTextColors(R.color.tabFirst, R.color.tabSecond);
        searchResultTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.tabSecond));
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        String name = getArguments().getString("name");

        List<String> tabList = new ArrayList<>();
        List<Fragment> fragmentList = new ArrayList<>();

        fragmentList.add(SrdFragment.newInstance(name));
        fragmentList.add(SrnFragment.newInstance(name));

        tabList.add("目的地");
        tabList.add("帖子");

        SrViewPagerAdapter srViewPagerAdapter = new SrViewPagerAdapter(getChildFragmentManager(), fragmentList, tabList);
        searchResultViewPager.setAdapter(srViewPagerAdapter);
        searchResultTabLayout.setupWithViewPager(searchResultViewPager);
        searchResultTabLayout.setTabsFromPagerAdapter(srViewPagerAdapter);
    }
}
